﻿using IntelligentGateway.Model;
using IntelligentGateway.Tools;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IntelligentGateway.Model.Platform;
using IntelligentGateway.Model.Attribute;
using IntelligentGateway.Data;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json.Linq;
using Microsoft.IdentityModel.Logging;
namespace IntelligentGateway.Services
{
    public class IGTCPServerMessageProcess
    {
        static int HeaderLength = 8;
        static int OpenCount = 0;
        static int CloseCount = 0;
        public static bool MessageUnPacket(byte[] data)
        {
            try
            {
                if (data.Length <= 0) return true;
                if (data[0] != 0xA7) return true;
                if (data[3] == 0x03)
                {
                    int type = 0;//0=楼层、方向数据1=梯控数据
                    if (data[4] == 0x03)
                    {
                        type = 1;//梯控数据
                    }

                    string deviceNO = BitConverter.ToUInt16(new byte[] { data[2], data[1] }, 0).ToString();
                    ushort datalength = BitConverter.ToUInt16(new byte[] { data[7], data[6] }, 0);
                    int carlinktotallength = data[8] + 1;
                    BitArray bits;
                    bool check = false;
                    bool closedoor = true;
                    bool openfinished = false;
                    bool closefinished = false;
                    bool opendoor = false;
                    bool up = false;
                    bool down = false;
                    bool fail = false;
                    bool lockelevator = false;
                    bool isolate = false;
                    bool fire = false;
                    bool stop = false;
                    bool control = false;
                    if (data[8] != 0)
                    {
                        byte[] carlink = new byte[data[8]];
                        Array.Copy(data, 9, carlink, 0, data[8]);
                        bits = new BitArray(carlink);//data[8]carlink长度;
                        opendoor = bits.Get(0);
                        if (opendoor)
                        {
                            OpenCount++;
                            closedoor = false;
                            CloseCount = 0;
                        }
                        else
                        {
                            CloseCount++;
                            OpenCount = 0;
                        }
                        if (OpenCount >= 2)
                        {
                            openfinished = true;
                        }
                        if (CloseCount >= 2)
                        {
                            closefinished = true;
                        }
                        bool bell = bits.Get(1);
                        bool driver = bits.Get(2);
                        check = bits.Get(3);
                        control = bits.Get(4);
                    }
                    int halllinklength = data[HeaderLength + carlinktotallength];
                    if (halllinklength > 0)
                    {
                        byte[] hlink = new byte[halllinklength];
                        Array.Copy(data, 9 + carlinktotallength, hlink, 0, data[7 + carlinktotallength + 1]);
                        bits = new BitArray(hlink);//data[8]carlink长度;

                        up = bits.Get(0);
                        down = bits.Get(1);
                        fail = bits.Get(2);
                        lockelevator = bits.Get(3);
                        isolate = bits.Get(4);
                        fire = bits.Get(5);
                        stop = fail || lockelevator || check || fire;
                    }
                    int glinklength = data[HeaderLength + carlinktotallength + halllinklength + 1];
                    int otherlength = data[HeaderLength + carlinktotallength + halllinklength + 1 + glinklength + 1];
                    char floorA = (char)0;
                    char floorB = (char)0;
                    int iFloor = 0;
                    if (otherlength > 0)
                    {
                        floorA = (char)data[HeaderLength + carlinktotallength + halllinklength + 1 + glinklength + 1 + 4];
                        floorB = (char)data[HeaderLength + carlinktotallength + halllinklength + 1 + glinklength + 1 + 5];
                        iFloor = int.Parse((floorA.ToString() + floorB.ToString()));
                    }
                    WebSocketRequest request = new WebSocketRequest();
                    List<liftStatusRequest> lst = new List<liftStatusRequest>();
                    liftStatusRequest liftStatusRequest = new liftStatusRequest();
                    liftStatusRequest.deviceID = deviceNO;
                    liftStatusRequest.smaplingTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    liftStatusRequest.dFloor = iFloor.ToString();
                    if (GlobalVariables.SignalPointList != null)
                    {
                        var device = GlobalVariables.DeviceList.Find(t => t.NO.ToString() == deviceNO);
                        if (device != null)
                        {
                            var item = GlobalVariables.SignalPointList.Find(t => t.TypeID == 0 && t.NO == device.SignType && t.DFloorID == iFloor.ToString());
                            if (item != null)
                            {
                                liftStatusRequest.pFloor = item.FloorID;
                            }
                        }
                    }

                    liftStatusRequest.direction = stop ? 0 : up ? 1 : down ? 2 : 3;
                    liftStatusRequest.door = openfinished ? 2 : closefinished ? 3 : opendoor ? 1 : closedoor ? 0 : 4;
                    liftStatusRequest.fault = fail ? 1 : 0;
                    liftStatusRequest.oss = stop ? 1 : 0;
                    liftStatusRequest.control = control ? 1 : 0;
                    liftStatusRequest.type = type;
                    lst.Add(liftStatusRequest);
                    request.req_id = UTools.GetCredNumber();
                    request.method = "liftStatus";
                    request.data = lst;
                    string result = JsonConvert.SerializeObject(request);
                    byte[] sendData = System.Text.Encoding.UTF8.GetBytes(result);
                    bool success = GlobalVariables.IntelligentWebSocketClient.SendData(sendData);
                    if (success)
                    {
                        foreach (var t in GlobalVariables.TPWebSocketIntPtrs)
                        {
                            GlobalVariables.IntelligentWebSocketServer.SendData(t, sendData, sendData.Length);
                        }
                        return true;
                    }
                    else
                    {
                        Log.Debug("IGTCPServerMessageProcess:MessageUnPacket:send error:" + Tools.UTools.ToHexs(sendData));
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return true;
            }
        }
        public static bool ProcessOutCall(SignalPoint Data)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                try
                {
                    string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                    var item = GlobalVariables.DeviceList.Find(t =>
                    {
                        if (t.DeviceIP.Equals(ipaddress)) return true;
                        else return false;
                    });
                    if (item != null)
                    {
                        byte[] data = new byte[13]
                        {
                    0xA7,0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x0D,0x01,0x00,0x00,0x00
                        };
                        data[1] = item.ByteNO[1];
                        data[2] = item.ByteNO[0];
                        byte value = ConvertValue(Data.RecordValue);
                        data[10] = value;
                        if (Data.RSL == "hlink")
                        {
                            data[8] = 0x0D;
                        }
                        else if (Data.RSL == "clink")
                        {
                            data[8] = 0x0C;
                        }
                        else
                        {
                            data[8] = 0x0E;
                        }
                        byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                        byte[] crcValue = UTools.ComputeCrc16(crcByte);
                        if (crcValue.Length == 2)
                        {
                            data[11] = crcValue[1];
                            data[12] = crcValue[0];
                        }
                        string cmd = UTools.ToHexs(data);
                        success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                    }
                }
                catch
                {
                    return true;
                }
            }
            return success;
        }
        public static bool ProcessInCall(SignalPoint Data)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                var item = GlobalVariables.DeviceList.Find(t =>
                {
                    if (t.DeviceIP.Equals(ipaddress)) return true;
                    else return false;
                });
                if (item != null)
                {
                    byte[] data = new byte[14]
                {
                    0xA7,0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x0C,0x02,0x20,0xA0,0xB0,0xE9
                };
                    data[1] = item.ByteNO[1];
                    data[2] = item.ByteNO[0];
                    byte value = ConvertValue(Data.RecordValue);
                    byte authvalue = ConvertValue(Data.AuthorizeValue);
                    if (Data.RSL == "hlink")
                    {
                        data[8] = 0x0D;
                    }
                    else if (Data.RSL == "clink")
                    {
                        data[8] = 0x0C;
                    }
                    else
                    {
                        data[8] = 0x0E;
                    }
                    data[9] = 0x02;
                    data[10] = value;
                    data[11] = authvalue;
                    byte[] crcByte = new byte[12] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10],data[11] };
                    byte[] crcValue = UTools.ComputeCrc16(crcByte);
                    if (crcValue.Length == 2)
                    {
                        data[11] = crcValue[1];
                        data[12] = crcValue[0];
                    }
                    success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                }
            }
            return success;
        }
        public static bool SmartProcessOutCall(SignalPoint Data, string ip)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                try
                {
                    string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                    if (ip == ipaddress)
                    {
                        var item = GlobalVariables.DeviceList.Find(t =>
                        {
                            if (t.DeviceIP.Equals(ipaddress)) return true;
                            else return false;
                        });
                        if (item != null)
                        {
                            byte[] data = new byte[13]
                            {
                    0xA7,0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x0D,0x01,0x00,0x00,0x00
                            };
                            data[1] = item.ByteNO[1];
                            data[2] = item.ByteNO[0];
                            byte value = ConvertValue(Data.RecordValue);
                            data[10] = value;
                            if (Data.RSL == "hlink")
                            {
                                data[8] = 0x0D;
                            }
                            else if (Data.RSL == "clink")
                            {
                                data[8] = 0x0C;
                            }
                            else
                            {
                                data[8] = 0x0E;
                            }
                            byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                            byte[] crcValue = UTools.ComputeCrc16(crcByte);
                            if (crcValue.Length == 2)
                            {
                                data[11] = crcValue[1];
                                data[12] = crcValue[0];
                            }
                            success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                        }
                    }
                }
                catch
                {
                    return true;
                }
            }
            return success;
        }
        public static bool SmartInCallMultiFloor(List<SignalPoint> Data, int type, string ip, int AuthorizeType)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();

            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);

                if (ip == ipaddress)
                {
                    var item = GlobalVariables.DeviceList.Find(t =>
                    {
                        if (t.DeviceIP.Equals(ipaddress)) return true;
                        else return false;
                    });
                    if (item != null)
                    {
                        if (type == 2)//点亮，脉冲
                        {
                            byte[] crcdata = new byte[10 + Data.Count * 2];
                            byte[] data = new byte[12 + Data.Count * 2];
                            crcdata[0] = 0xA7;
                            crcdata[1] = item.ByteNO[1];
                            crcdata[2] = item.ByteNO[0];
                            crcdata[3] = 0x10;
                            crcdata[4] = 0x01;
                            crcdata[5] = 0x00;
                            crcdata[6] = 0x00;
                            crcdata[7] = BitConverter.GetBytes(Data.Count * 2 + 2)[0];

                            if (Data[0].RSL == "hlink")
                            {
                                crcdata[8] = 0x0D;
                            }
                            else if (Data[0].RSL == "clink")
                            {
                                crcdata[8] = 0x0C;
                            }
                            else
                            {
                                crcdata[8] = 0x0E;
                            }
                            crcdata[9] = BitConverter.GetBytes(Data.Count * 2)[0];
                            for (int i = 0; i < Data.Count; i++)
                            {
                                byte value = ConvertValue(Data[i].RecordValue);
                                byte authvalue = ConvertValue(Data[i].AuthorizeValue);
                                crcdata[10 + i * 2] = value;
                                crcdata[11 + i * 2] = authvalue;
                            }
                            byte[] crcValue = UTools.ComputeCrc16(crcdata);
                            if (crcValue.Length == 2)
                            {
                                Array.Copy(crcdata, 0, data, 0, 10 + Data.Count * 2);
                                data[10 + Data.Count * 2] = crcValue[1];
                                data[11 + Data.Count * 2] = crcValue[0];
                            }
                            string cmd = UTools.ToHexs(data);
                            success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                        }
                        else//授权，置位
                        {
                            byte[] crcdata = new byte[10 + Data.Count];
                            byte[] data = new byte[12 + Data.Count];
                            crcdata[0] = 0xA7;
                            crcdata[1] = item.ByteNO[1];
                            crcdata[2] = item.ByteNO[0];
                            crcdata[3] = 0x10;
                            crcdata[4] = 0x01;
                            crcdata[5] = 0x00;
                            crcdata[6] = 0x00;
                            crcdata[7] = BitConverter.GetBytes(Data.Count + 2)[0];
                            if (Data[0].RSL == "hlink")
                            {
                                crcdata[8] = 0x05;
                            }
                            else if (Data[0].RSL == "clink")
                            {
                                crcdata[8] = 0x04;
                            }
                            else
                            {
                                crcdata[8] = 0x06;
                            }
                            crcdata[9] = BitConverter.GetBytes(Data.Count)[0];
                            for (int i = 0; i < Data.Count; i++)
                            {
                                byte authvalue = ConvertValue(Data[i].AuthorizeValue);
                                crcdata[10 + i] = authvalue;
                            }
                            byte[] crcValue = UTools.ComputeCrc16(crcdata);
                            if (crcValue.Length == 2)
                            {
                                Array.Copy(crcdata, 0, data, 0, 10 + Data.Count);
                                data[10 + Data.Count] = crcValue[1];
                                data[11 + Data.Count] = crcValue[0];
                            }
                            success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                            if (success)
                            {
                                foreach (var point in Data)
                                {
                                    SignalPointClear pointClear = new SignalPointClear();
                                    pointClear.deviceid = item.NO;
                                    pointClear.deviceip = item.DeviceIP;
                                    pointClear.FloorID = point.FloorID;
                                    pointClear.AuthorizeValue = point.AuthorizeValue;
                                    pointClear.ClearTime = DateTime.Now;
                                    pointClear.AuthorizeType = AuthorizeType;
                                    pointClear.RSL = point.RSL;
                                    var pointitem = GlobalVariables.SignalPointClearList.Find(t =>
                                    {
                                        if (t.deviceid == pointClear.deviceid && t.FloorID == pointClear.FloorID) return true;
                                        else return false;
                                    });
                                    if (pointitem != null)
                                    {
                                        GlobalVariables.SignalPointClearList.Remove(pointitem);
                                    }
                                    GlobalVariables.SignalPointClearList.Add(pointClear);
                                }
                            }
                        }
                    }
                }
            }
            return success;
        }
        public static bool ProcessPointClear(SignalPointClear signalPoint)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                if (signalPoint.deviceip == ipaddress)
                {
                    var item = GlobalVariables.DeviceList.Find(t =>
                    {
                        if (t.DeviceIP.Equals(ipaddress)) return true;
                        else return false;
                    });
                    if (item != null)
                    {
                        byte[] data = new byte[13]
                         {
                    0xA7,0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x0D,0x01,0x00,0x00,0x00
                         };
                        data[1] = item.ByteNO[1];
                        data[2] = item.ByteNO[0];
                        byte value = ConvertValue(signalPoint.AuthorizeValue);
                        data[10] = value;
                        if (signalPoint.RSL == "hlink")
                        {
                            data[8] = 0x09;
                        }
                        else if (signalPoint.RSL == "clink")
                        {
                            data[8] = 0x08;
                        }
                        else
                        {
                            data[8] = 0x0A;
                        }
                        byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                        byte[] crcValue = UTools.ComputeCrc16(crcByte);
                        if (crcValue.Length == 2)
                        {
                            data[11] = crcValue[1];
                            data[12] = crcValue[0];
                        }
                        string cmd = UTools.ToHexs(data);
                        Log.Debug($"Data:{cmd}");
                        success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                    }
                }
            }
            return success;
        }
        public static bool ProcessKeepOpenDoor(SignalPoint Data)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                var item = GlobalVariables.DeviceList.Find(t =>
                {
                    if (t.DeviceIP.Equals(ipaddress)) return true;
                    else return false;
                });
                if (item != null)
                {
                    byte[] data = new byte[13]
                    {
                       0xA7, 0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x04,0x01,0x20,0x43,0x03
                     };
                    data[1] = item.ByteNO[1];
                    data[2] = item.ByteNO[0];
                    byte value = ConvertValue(Data.RecordValue);
                    data[9] = 0x01;
                    data[10] = value;
                    byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                    byte[] crcValue = UTools.ComputeCrc16(crcByte);
                    if (crcValue.Length == 2)
                    {
                        data[11] = crcValue[0];
                        data[12] = crcValue[1];
                    }
                    success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                }
            }
            return success;
        }
        public static bool ProcessCancelOpenDoor(SignalPoint Data)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                var item = GlobalVariables.DeviceList.Find(t =>
                {
                    if (t.DeviceIP.Equals(ipaddress)) return true;
                    else return false;
                });
                if (item != null)
                {
                    byte[] data = new byte[13]
                    {
                        0xA7, 0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x08,0x01,0x20,0x83,0x03
                    };
                    data[1] = item.ByteNO[0];
                    data[2] = item.ByteNO[1];
                    byte value = ConvertValue(Data.RecordValue);
                    data[9] = 0x02;
                    data[10] = value;
                    byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                    byte[] crcValue = UTools.ComputeCrc16(crcByte);
                    if (crcValue.Length == 2)
                    {
                        data[11] = crcValue[0];
                        data[12] = crcValue[1];
                    }
                    success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                }
            }
            return success;
        }
        public static bool EnableLiftControl(SignalPoint Data, string ip)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                if (ip == ipaddress)
                {
                    var item = GlobalVariables.DeviceList.Find(t =>
                    {
                        if (t.DeviceIP.Equals(ipaddress)) return true;
                        else return false;
                    });
                    if (item != null)
                    {
                        byte[] data = new byte[13]
                    {
                    0xA7, 0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x04,0x01,0x20,0x43,0x03
                    };
                        data[1] = item.ByteNO[1];
                        data[2] = item.ByteNO[0];
                        if (Data.RSL == "hlink")
                        {
                            data[8] = 0x05;
                        }
                        else if (Data.RSL == "clink")
                        {
                            data[8] = 0x04;
                        }
                        else
                        {
                            data[8] = 0x06;
                        }
                        byte value = ConvertValue(Data.RecordValue);
                        data[10] = value;
                        byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                        byte[] crcValue = UTools.ComputeCrc16(crcByte);
                        if (crcValue.Length == 2)
                        {
                            data[11] = crcValue[1];
                            data[12] = crcValue[0];
                        }
                        success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                    }
                }
            }
            return success;
        }
        public static bool SmartEnableLiftControl(int deviceid)
        {
            SignalPointDA signalPointDA = new SignalPointDA();
            SignalPoint signalPoint = signalPointDA.GetSignalPointData("梯控控制", deviceid);
            var item = GlobalVariables.DeviceList.Find(t =>
            {
                if (t.NO == deviceid) return true;
                else return false;
            });
            if (item != null)
            {
                return EnableLiftControl(signalPoint, item.DeviceIP);
            }
            else
            {
                return true;
            }
        }
        public static bool DisableLiftControl(SignalPoint Data, string ip)
        {
            bool success = true;
            List<IntPtr> lstPtrs = GlobalVariables.IGTCPServer.GetAllIntPtrs();
            foreach (var intptr in lstPtrs)
            {
                string ipaddress = GlobalVariables.IGTCPServer.GetIpAddress(intptr);
                if (ip == ipaddress)
                {
                    var item = GlobalVariables.DeviceList.Find(t =>
                    {
                        if (t.DeviceIP.Equals(ipaddress)) return true;
                        else return false;
                    });
                    if (item != null)
                    {
                        byte[] data = new byte[13]
                    {
                    0xA7,0x00,0x01,0x10,0x01,0x00,0x00,0x03,0x08,0x01,0x20,0x43,0x03
                    };
                        data[1] = item.ByteNO[1];
                        data[2] = item.ByteNO[0];
                        if (Data.RSL == "hlink")
                        {
                            data[8] = 0x09;
                        }
                        else if (Data.RSL == "clink")
                        {
                            data[8] = 0x08;
                        }
                        else
                        {
                            data[8] = 0x0A;
                        }
                        byte value = ConvertValue(Data.RecordValue);
                        data[10] = value;
                        byte[] crcByte = new byte[11] { data[0], data[1], data[2], data[3],
                                            data[4], data[5], data[6], data[7],
                                            data[8], data[9], data[10] };
                        byte[] crcValue = UTools.ComputeCrc16(crcByte);
                        if (crcValue.Length == 2)
                        {
                            data[11] = crcValue[1];
                            data[12] = crcValue[0];
                        }
                        success &= GlobalVariables.IGTCPServer.SendData(intptr, data);
                    }
                }
            }

            return success;
        }
        public static bool SmartDisableLiftControl(int deviceid)
        {
            SignalPointDA signalPointDA = new SignalPointDA();
            SignalPoint signalPoint = signalPointDA.GetSignalPointData("梯控控制", deviceid);
            var item = GlobalVariables.DeviceList.Find(t =>
            {
                if (t.NO == deviceid) return true;
                else return false;
            });
            if (item != null)
            {
                return DisableLiftControl(signalPoint, item.DeviceIP);
            }
            else
            {
                return true;
            }


        }
        public static byte ConvertValue(string Value)
        {
            string[] str = Value.Split(',');
            if (str.Length != 2) { return 0; }
            byte addressByte = (byte)(byte.Parse(str[0]) << 2);
            byte addressBit = (byte)(byte.Parse(str[1]) - 1);
            byte newValue = (byte)((addressByte) + addressBit);
            return newValue;

        }

        public static bool QRCodeProcess(byte[] data)
        {
            try
            {
                int ladder_id = data[6];

                if (data[9] == 0x37)
                {
                    int qx_data = data[14];
                    string qx_str = Convert.ToString(qx_data, 2).PadLeft(8, '0');
                    bool qx_value = qx_str.Substring(7, 1) == "1" ? true : false;

                    if (ladder_id == 0)
                    {
                        foreach (var item in GlobalVariables.DeviceList)
                        {
                            if (qx_value)
                            {
                                SmartEnableLiftControl(item.NO);
                            }
                            else
                            {
                                SmartDisableLiftControl(item.NO);
                            }
                            Wait(200);
                        }
                        return true;
                    }
                    else
                    {
                        if (qx_value)
                        {
                            return SmartEnableLiftControl(ladder_id);
                        }
                        else
                        {
                            return SmartDisableLiftControl(ladder_id);
                        }
                    }
                }
                else
                {
                    byte[] front_data = new byte[10];
                    Array.Copy(data, 13, front_data, 0, 10);
                    byte[] back_data = new byte[10];
                    Array.Copy(data, 23, back_data, 0, 10);

                    string frontfloor = FaceDataProcess(front_data);//前门物理楼层
                    string backfloor = FaceDataProcess(back_data);//后门物理楼层
                    int type = 0;//
                    if (data[9] == 0x0F)//授权
                    {
                        type = 1;
                    }
                    else if (data[9] == 0x19)//点亮
                    {
                        type = 2;
                    }
                    SignalPointDA signalPointDA = new SignalPointDA();
                    List<SignalPoint> signalPoints = new List<SignalPoint>();
                    if (!string.IsNullOrEmpty(frontfloor))
                    {
                        List<SignalPoint> front_point = signalPointDA.GetSignalPFloorPointListData(0, frontfloor, ladder_id.ToString());
                        if (front_point != null && front_point.Count > 0)
                        {
                            signalPoints.AddRange(front_point);
                        }
                    }
                    if (!string.IsNullOrEmpty(backfloor))
                    {
                        List<SignalPoint> back_point = signalPointDA.GetSignalPFloorPointListData(3, backfloor, ladder_id.ToString());
                        if (back_point != null && back_point.Count > 0)
                        {
                            signalPoints.AddRange(back_point);
                        }
                    }

                    if (ladder_id == 0)
                    {
                        foreach (var item in GlobalVariables.DeviceList)
                        {
                            SendInbound(signalPoints, type, item.NO);
                            Wait(200);
                        }
                    }
                    else
                    {
                        SendInbound(signalPoints, type, ladder_id);
                    }
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public static bool Wait(int second)
        {
            try
            {
                DateTime startTime = DateTime.Now;
                while (true)
                {
                    TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - startTime.Ticks);
                    if (secondSpan.TotalMilliseconds > second)
                    {
                        break;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool SendInbound(List<SignalPoint> list, int IsAuthorize, int DeviceId)
        {
            var item = GlobalVariables.DeviceList.Find(t =>
            {
                if (t.NO == DeviceId) return true;
                else return false;
            });
            if (item != null)
            {
                return IGTCPServerMessageProcess.SmartInCallMultiFloor(list, IsAuthorize, item.DeviceIP, 1);
            }
            else
            {
                return true;
            }
        }

        #region 人脸数据解析
        public static string FaceDataProcess(byte[] bytes)
        {
            byte[] data_byte = new byte[bytes.Length];
            for (int n = 0; n < bytes.Length; n++)
            {
                if (n % 2 == 0)
                {
                    data_byte[8 - n] = bytes[n];
                }
                else
                {
                    data_byte[10 - n] = bytes[n];
                }
            }

            string add_string = string.Join("", data_byte.Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));
            string floor_data = GetPhysicalFloor(add_string);
            return floor_data;
        }

        public static string GetPhysicalFloor(string data)
        {
            string decimalValue = string.Empty;
            for (int i = 0; i < data.Length; i++)
            {
                if (data.Substring(data.Length - 1 - i, 1) == "1")
                {
                    decimalValue += (i + 1).ToString() + ",";
                }
            }
            if (!string.IsNullOrEmpty(decimalValue))
            {
                decimalValue = decimalValue.Substring(0, decimalValue.Length - 1);
            }
            return decimalValue;
        }
        #endregion
    }
}
